namespace PHSRAG.Insight21.FormsManagement.Controls
{
	using System;
	using System.Text;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.ComponentModel;

	/// <summary>
	///	MultipleSelection is the UI displayed to the user when a MultiSelectQuestion is rendered
	/// </summary>
	public partial class Migrated_MultipleSelection : MultipleSelection
	{
		private string parentName;
		private string parentValue;
		private string items;
		private string selectedListItemText;
		private bool readOnly;

		/// <summary>
		/// Gets or sets the parent question name for the MultiInput question
		/// </summary>
		override public string ParentName
		{
			get { return parentName; }
			set { parentName = value; }
		}


		/// <summary>
		/// Gets or sets the parent value for the MultiInput question
		/// </summary>
		override public string ParentValue
		{
			get { return parentValue; }
			set { parentValue = value; }
		}


		/// <summary>
		/// Gets or sets the '^' delimited list of values for the listbox in MultiInput control
		/// </summary>
		override public string Items
		{
			get 
			{
				int itemCount = itemsList.Items.Count;
				StringBuilder items = new StringBuilder();
				for (int i=0; i<itemCount; i++)
				{
					items.Append(string.Format("{0}^", itemsList.Items[i].Text));
				}
				string allItems = items.ToString();
				if (allItems.Length > 0)
					allItems = allItems.Substring(1, allItems.Length - 1);
				return allItems;
			}
			set
			{
				items = value;
				
			}
		}


		/// <summary>
		/// Gets or sets the selected item in the list box
		/// </summary>
		override public string SelectedListItemText
		{
			get
			{
				return (itemsList.SelectedIndex >=0) ? itemsList.SelectedItem.Text : string.Empty;
			}
			set
			{
				selectedListItemText = value;
			}
		}

		/// <summary>
		/// Sets whether or not the control is readonly. For readonly controls, the textbox and '>' will not be visble.
		/// </summary>
		public bool ReadOnly
		{
			set
			{
				readOnly = value;
			}
		}

		/// <summary>
		/// Loads options already entered by the user, if any, into the list box. Each option has an associated text that is visible to the user and value that uniquely 
		/// identifies the option.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnPageLoad(object sender, System.EventArgs e)
		{
			itemsList.Items.Clear();
			string[] itemArray = items.Split('^');
			int itemCount = itemArray.Length;
			for (int i=0; i<itemCount; ++i)
			{
				if (itemArray[i] != string.Empty)
				{
					itemsList.Items.Add(itemArray[i]);
					if (itemArray[i] == selectedListItemText)
					{
						itemsList.SelectedIndex = i;
					}
				}
			}
				
			if (itemsList.Items.Count>0)
			{
				removeItem.Visible = true;
			}
		}


		#region Web Form Designer generated code
		/// <summary>
		/// Page Initialization
		/// </summary>
		/// <param name="e">Arguments associated with the event.</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			
		    base.OnInit(e);
			InitializeComponent();
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.OnPageLoad);

		}
		#endregion

		/// <summary>
		/// If not already added, adds the new option entered to the list box. This option is also added to the answers list of the corresponding question.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnAddItemClick(object sender, EventArgs e)
		{
			string newItemText = newItem.Text.Trim();
			if(newItemText != "" && itemsList.Items.FindByText(newItemText) == null)
			{
				itemsList.Items.Add(newItemText);
				removeItem.Visible = true;
				newItem.Text = string.Empty;
				QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
				viewer.AddMultiSelectItem(parentName, parentValue, ID.Substring(ID.IndexOf("-") + 1), newItemText);
			}
		}


		/// <summary>
		/// Removes the selected option from the list box. This option is also deleted from the answers list of the corresponding question.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnRemoveItemClick(object sender, EventArgs e)
		{
			int selectedIndex = itemsList.SelectedIndex;
			if (selectedIndex >= 0)
			{
				string removedItem = itemsList.SelectedItem.Text;
				itemsList.Items.RemoveAt(selectedIndex);
				if (itemsList.Items.Count == 0)
				{
					removeItem.Visible = false;
				}
				QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
				viewer.RemoveMultiSelectItem(parentName, parentValue, ID.Substring(ID.IndexOf("-") + 1), removedItem);
			}
		}


		/// <summary>
		/// Based on the option selected, the answers for the child questions, if any, are displayed.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnItemsListSelectedIndexChanged(object sender, System.EventArgs e)
		{
			QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
			viewer.Refresh();
		}

		/// <summary>
		/// PreRender
		/// </summary>
		/// <param name="sender">Object that raised the event</param>
		/// <param name="e">Arguments associated with the event</param>
		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (readOnly)
			{
				newItemCell.Visible = false;
				addItemCell.Visible = false;
				removeItemCell.Visible = false;
			}
		}

	}
}
